package cn.pxs.controller;

import cn.pxs.entity.Blogs;
import cn.pxs.entity.BlogsAndContent;
import cn.pxs.entity.User;
import cn.pxs.service.BlogsService;
import cn.pxs.service.TypeService;
import cn.pxs.service.UserService;
import cn.pxs.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/public")
public class PublicController {

    @Autowired
    UserService userService;
    @Autowired
    BlogsService blogsService;
    @Autowired
    TypeService typeService;


    @RequestMapping("blogs/findBlogs")
    @ResponseBody
    public Map findBlogs(){
        List<Blogs> lst = blogsService.findBlogs();
        return Tools.RespJson(200,"",lst);
    }

    @RequestMapping("blogs/findByTypeId/{typeid}")
    @ResponseBody
    public Map findByTypeId(@PathVariable("typeid") String typeid){
        List<Blogs> lst = blogsService.findByTypeId(typeid);
        return Tools.RespJson(200,"",lst);
    }


    @RequestMapping("user/add")
    @ResponseBody
    public Map add(@RequestBody User user){
        userService.add(user);
        return Tools.RespJson(200,"",null);
    }


    @RequestMapping("blogs/findById/{id}")
    @ResponseBody
    public Map findById(@PathVariable("id") String id){
        BlogsAndContent bc = blogsService.findById(id);
        return Tools.RespJson(200,"",bc);
    }


    @RequestMapping("type/findAll")
    @ResponseBody
    public Map findTypeAll(){
        return Tools.RespJson(200,"",typeService.findAll());
    }

    @RequestMapping("type/findById/{typeid}")
    @ResponseBody
    public Map typeFindById(@PathVariable("typeid")String id){
        return Tools.RespJson(200,"",typeService.findById(id));
    }



    @RequestMapping("/goFindBlog")
    public String goFindBlog(){
        return "blogs/find";
    }

    @RequestMapping("/goAddUser")
    public String goAddUser(){
        return "user/add";
    }

    @RequestMapping("/goLogin")
    public String goLogin(){
        return "login/login";
    }

    @RequestMapping("goIndex")
    public String goIndex(){
        return "index";
    }



}
